package win.smartown.aqst.demo.contract;

import android.util.Log;

import com.google.gson.Gson;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import win.smartown.aqst.demo.entity.Province;
import win.smartown.aqst.demo.entity.Response;
import win.smartown.aqst.demo.net.ApiService;
import win.smartown.aqst.demo.net.RequestListener;
import win.smartown.aqst.mvp.BasePresenter;
import win.smartown.aqst.mvp.StateView;

/**
 * @author 雷小武
 * 创建时间：2018/11/6 13:47
 * 版权：成都智慧一生约科技有限公司
 * 类描述：
 */
public interface MainContract {

    class Presenter extends BasePresenter<View> {

        public Presenter(View view) {
            super(view);
        }

        public void getWeather() {
            mView.startLoading(true);
            execute(Observable.just("")
                            .delay(3, TimeUnit.SECONDS)
                            .concatMap(new Function<String, Observable<Response<List<Province>>>>() {
                                @Override
                                public Observable<Response<List<Province>>> apply(String s) throws Exception {
                                    return ApiService.getWeatherService().getProvince("186c3444d08624f3eaf647bbb916cd21");
                                }
                            }),
                    new RequestListener<List<Province>>() {
                        @Override
                        public void onSuccess(List<Province> provinces) {
                            Log.i("getProvince", new Gson().toJson(provinces));
                            mView.loadSuccess();
                        }

                        @Override
                        public void onFail(Throwable e) {
                            Log.e("getProvince", "onFail:" + e.getMessage());
                            mView.loadFail();
                        }

                        @Override
                        public void onFinish() {
                            Log.i("getProvince", "onFinish");
                        }
                    });
        }
    }

    interface View extends StateView {

    }

}